package com.app.community.mapper;

import com.app.community.dto.NotificationDTO;
import com.app.community.model.entity.Notification;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * created by czy on 2020/11/20 9:35
 **/
@Mapper
public interface NotificationMapper {

    /**
     *  插入通知,包含点赞和评论
     */
    @Insert({"insert into notification(question_id, notifier, receiver, type, status,date) values (#{questionId},#{notifier},(select creator from question where question_id = #{questionId}),#{type},#{status},#{date})"})
    void insertNotification(@Param("questionId")Long questionId,@Param("notifier")String notifier,@Param("type")Integer type,@Param("status")Integer status,@Param("date")String date);

    /**
     * 更新状态为已读
     */
    @Update({"update notification set status = #{status} where receiver = #{receiver}"})
    void updateNotificationStatus(@Param("status")Integer status,@Param("receiver")String receiver);

    /**
     * 删除通知
     */
    @Delete({"delete from notification where question_id = #{questionId} and notifier = #{notifier} and status = #{status} and type = #{type}"})
    void deleteNotification(@Param("questionId")Long questionId,@Param("notifier")String notifier,@Param("status")Integer status,@Param("type")Integer type);


    /**
     * 查询某用户某种类型的通知
     */
    @Results({
            @Result(column = "nick_name",property = "notifier"),
            @Result(column = "question_id",property = "questionId")
    })
    @Select({"select u.nick_name, q.title,n.question_id from notification n,user u,question q where receiver = #{receiver} and type = #{type} and q.question_id = n.question_id and u.user_id = n.notifier and n.status = #{status} order by n.date desc "})
    List<NotificationDTO> selectNotification(@Param("receiver")String receiver, @Param("type")Integer type, @Param("status")Integer status);

    /**
     * 查询用户未读消息
     */
    @Select({"select count(*) from notification where receiver = #{receiver} and status = #{status}"})
    Integer countUNREADNotification(@Param("receiver")String receiver,@Param("status")Integer status);

}
